package api

import (
	"go-weiqi-system/config"
	"go-weiqi-system/internal/models"
	"go-weiqi-system/internal/utils"

	"github.com/gin-gonic/gin"
)

type SuggestionController struct{}

// Submit 提交建议
func (ctrl *SuggestionController) Submit(c *gin.Context) {
	userID, _ := c.Get("user_id")

	var req struct {
		Content string `json:"content" binding:"required"`
		Contact string `json:"contact"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		utils.Error(c, "参数错误")
		return
	}

	suggestion := models.Suggestion{
		UserID:  userID.(uint),
		Content: req.Content,
		Contact: req.Contact,
		Status:  "待处理",
	}

	if err := config.DB.Create(&suggestion).Error; err != nil {
		utils.Error(c, "提交失败")
		return
	}

	utils.Success(c, suggestion, "提交成功")
}

// MyList 我的建议列表
func (ctrl *SuggestionController) MyList(c *gin.Context) {
	userID, _ := c.Get("user_id")

	var param models.PageParam
	if err := c.ShouldBindQuery(&param); err != nil {
		param.Page = 1
		param.PageSize = 10
	}

	var suggestions []models.Suggestion
	var total int64

	db := config.DB.Model(&models.Suggestion{}).Where("user_id = ?", userID)

	db.Count(&total)
	db.Order("id desc").
		Offset(param.GetOffset()).
		Limit(param.GetPageSize()).
		Find(&suggestions)

	utils.Success(c, models.PageResponse{
		List:     suggestions,
		Total:    total,
		Page:     param.GetPage(),
		PageSize: param.GetPageSize(),
	})
}
